package com.yk.airline.component.ExcelExport;


import com.yk.airline.component.ExcelExport.api.IExcelExport;
import com.yk.airline.dao.capturemark.api.ICaptureMarkDao;
import com.yk.airline.dao.capturewebsite.api.ICaptureWebsiteDao;
import com.yk.airline.model.CaptureMarkModel;
import com.yk.airline.model.CaptureWebsiteModel;
import com.yk.exportExcel.entity.Template;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Project Name:airlinecrawler
 * File Name:ExcelExportUtil
 * Package Name:com.yk.airline.component.ExcelExportImpl
 * Date:2018/2/1 21:13
 * Author:wangzhili
 * Description:
 * Copyright (c) 2018, 重庆云凯科技有限公司 All Rights Reserved.
 */
@Component("excelExportUtil")
public class ExcelExportUtil {

    @Resource(name = "captureMarkDao")
    private ICaptureMarkDao captureMarkDao;

    @Resource(name = "captureWebsiteDao")
    private ICaptureWebsiteDao captureWebsiteDao;

    @Resource(name = "excelExport")
    private IExcelExport excelExport;

    public List<String> run(List<String> airCompanies, Template template, String path) {

        /* 查到最近一次的批次对象以及获取它的爬取时间T */
        CaptureMarkModel lastCaptureMarkModel = this.captureMarkDao.findTheLatestMark();
        System.out.println("批次号 " + lastCaptureMarkModel.getId());
        Timestamp timestamp = lastCaptureMarkModel.getCaptureTime();
        Calendar lastMarkTime = transfer(timestamp);


        /* 查到网站集合 */
        String website;
        List<String> websites = new ArrayList<>();
        List<CaptureWebsiteModel> captureWebsiteModelList = this.captureWebsiteDao.findAll();
        for (CaptureWebsiteModel captureWebsiteModel : captureWebsiteModelList) {
            website = captureWebsiteModel.getName();
            websites.add(website);
        }

        /* 内置输出流 */
        OutputStream os = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
        List<String> fileList = new ArrayList<>();
        String captureTime = sdf.format(lastCaptureMarkModel.getCaptureTime().getTime());
        try {
            String templateOne = path + "/开舱最低价" + captureTime + ".xlsx";
            String templateTwo = path + "/特惠及优选" + captureTime + ".xlsx";
            fileList.add(templateOne);
            fileList.add(templateTwo);
            if ("TemplateOne".equals(template.toString())) {
                os = new FileOutputStream(templateOne);
            } else {
                os = new FileOutputStream(templateTwo);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        /* 调用方法 */
        this.excelExport.exportExcel(lastCaptureMarkModel, websites, airCompanies, lastMarkTime, template, os);

        /*关闭输出流*/
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileList;
    }

    /**
     * Timestamp格式转化为Calendar日期
     *
     * @param ts Timestamp格式的日期
     * @return java.util.Calendar
     */
    private Calendar transfer(Timestamp ts) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sdate = sdf.format(ts);
            Date date = sdf.parse(sdate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
